/**
 * 
 */
package com.johnny.xia.filetest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.johnny.xia.util.file.AppendToFile;

/**
 * @author johnnyxia
 *
 */
public class NginxLog{
    
    public static void main(String[] args){
        String s = "http://log-mshk.baozun.cn/frontend1/daily.log.";//2016-11-23.log.gz
        for(int i=1;i<=31;i++){
            if(i < 10){
                System.out.println(s+"2016-12-0"+i+".log.gz");
            }else{
                System.out.println(s+"2016-12-"+i+".log.gz"); 
            }
        }
        
    }
    
    public static void main2(String[] args){
        String file = "nginx_access.log";
        String file2="access.log";
        String path = "D:\\mclog\\20170102\\"+file2;
        String path2 = "D:\\mclog\\20170102\\access_bak.log";
//        Map<String,List<String>> dataMap = readFileByLines(path2+file);
//        
//        for(Map.Entry<String, List<String>> entry : dataMap.entrySet()){
//            
//            List<String> list = entry.getValue();
//            
//            for(String s : list){
//                AppendToFile.appendMethodA(path2+file2, entry.getKey()+" - - "+s+"\r\n");
//            }
//           
//        }
        
        
        String string = readFileByLines2(path);
        AppendToFile.appendMethodA(path2, string);
        
//        fw(path2+file, fileStr);
        
//        String s = "    at org.owasp.esapi.reference.validation.StringValidationRule.checkLength(StringValidationRule.java:214)";
        
//        System.out.println(s.startsWith(" "));
//        System.out.println(isok(" atsdfsdfsdf"));
        
    }
    
    
    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static String readFileByLines2(String fileName){

        StringBuffer buffer = new StringBuffer();

        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null){
                if(!tempString.contains("GET /oauth")){
                    continue;
                }
                buffer.append(splitStr(tempString)).append("\r\n");
               
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        return buffer.toString();
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static Map<String,List<String>> readFileByLines(String fileName){

        Map<String,List<String>> map = new HashMap<>();
        
        StringBuffer buffer = new StringBuffer();

        File file = new File(fileName);
        BufferedReader reader = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null){
//                if(!isok(tempString)){
//                    continue;
//                }
//                buffer.append(splitStr(tempString)).append("\r\n");
                
                String[] array = tempString.split(" - - ");
                
                if(map.get(array[0]) == null){
                    List<String> list = new ArrayList<>();
                    list.add(array[1]);
                    map.put(array[0], list);
                }else{
                    map.get(array[0]).add(array[1]);
                }
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        return map;
    }
    
    public static boolean isok(String str){
        
        if(str.contains("Exception")){
            return false;
        }
        if(str.contains("Log4JLogger.java:449")){
            return false;
        }
        
        if(str.length() < 8){
            return false;
        }
        
       /* String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;*/
        
        str = str.replace("2016-12-31 ", "");
        
        String tmp = str.substring(0,8);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        try{
            df.parse(tmp);
        }catch (ParseException e){
            return false;
        }
        return true;
    }
    
    public static String splitStr(String str){
        if(str.contains("PaymentController.java:472")){
            return str.split("\\,address")[0];
        }else if(str.contains("PaymentController.java:197")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:214")){
            return str.split("\\,result")[0];
        }else if(str.contains("PaymentController.java:223")){
            return str.split("\\,value")[0];
        }else if(str.contains("PaymentController.java:250")){
            return str.split("\\,value")[0];
        }
        return str;
    }
    
    public static void fw(String file ,String s){
        FileOutputStream out = null;   

        try {   

            out = new FileOutputStream(new File(file));   
                out.write(s.getBytes());   
            out.close();   


        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
